/**
 * jims
 */
package com.jims.master.modules.csm.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.csm.dao.*;
import com.jims.master.modules.csm.entity.*;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 消耗品盘点Service
 * @author zhuq
 * @version 2017-03-28
 */
@Service
@Transactional(readOnly = true)
public class ExpInventoryCheckService extends CrudService<ExpInventoryCheckDao, ExpInventoryCheck> {

    @Autowired
    private ExpStockDao expStockDao;

    @Autowired
    private ExpImportMasterService expImportMasterService;

    @Autowired
    private ExpImportDetailService expImportDetailService;

    @Autowired
    private ExpImportDetailDao expImportDetailDao;

    @Autowired
    private ExpImportMasterDao expImportMasterDao;

    @Autowired
    private ExpExportMasterDao expExportMasterDao;

    @Autowired
    private ExpExportService expExportService;

    @Autowired
    private ExpExportDetailDao expExportDetailDao;

	public ExpInventoryCheck get(String id) {
		return super.get(id);
	}
	
	public List<ExpInventoryCheck> findList(ExpInventoryCheck expInventoryCheck) {
		return super.findList(expInventoryCheck);
	}

    /**
     * 根据条件查询当月消耗品盘点盈亏统计
     * @param page
     * @param expInventoryCheck
     * @return
     * @author CTQ
     */
    public Page<ExpInventoryCheck> findProfigLossPage(Page<ExpInventoryCheck> page, ExpInventoryCheck expInventoryCheck) {
        List<ExpInventoryCheck> list = dao.findListProfitAndLoss(expInventoryCheck);
        page.setCount(list.size());
        page.setList(list);
        return page;
    }
	
	@Transactional(readOnly = false)
	public int save(ExpInventoryCheck expInventoryCheck) {
		return super.save(expInventoryCheck);
	}
	
	@Transactional(readOnly = false)
	public int delete(ExpInventoryCheck expInventoryCheck) {
		return super.delete(expInventoryCheck);
	}

    /*
    * 消耗品盘点，将一个消耗品不同批次的盘点数据保存
    * ZhuQ
    * */
    @Transactional(readOnly = false)
    public int saveList(List<ExpInventoryCheck> expInventoryCheckList) {
        //组装盘点入库记录与盘点出库记录数据
        List<ExpStock>expStockOutList=new ArrayList<ExpStock>();
        List<ExpStock>expStockInList=new ArrayList<ExpStock>();

        int num=1;
        Date inventoryDate =new Date();
        for(int i=0;i<expInventoryCheckList.size();i++){
            //实盘数大于库存数，组成盘点入库数据
            ExpStock expStock =new ExpStock();
            expStock =expStockDao.get(expInventoryCheckList.get(i).getStockId());//根据库存表id获取库存表数据
            if(expInventoryCheckList.get(i).getActualQuantity()>expInventoryCheckList.get(i).getMinQuantity()){
                expStock.setQuantity(expInventoryCheckList.get(i).getActualQuantity());
                num=num*expStockDao.update(expStock);
                expStock.setQuantity(expInventoryCheckList.get(i).getActualQuantity()-expInventoryCheckList.get(i).getMinQuantity());
                expStockInList.add(expStock);
            //实盘数大于库存数，组成盘点出库数据
            }else if(expInventoryCheckList.get(i).getActualQuantity()<expInventoryCheckList.get(i).getMinQuantity()){
                expStock.setQuantity(expInventoryCheckList.get(i).getActualQuantity());
                expStock.preUpdate();
                num=num*expStockDao.update(expStock);
                expStock.setQuantity(expInventoryCheckList.get(i).getMinQuantity()-expInventoryCheckList.get(i).getActualQuantity());
                expStockOutList.add(expStock);
            }
            //给expInventoryCheck 组装数据
            ExpInventoryCheck expInventoryCheck=new ExpInventoryCheck();
            expInventoryCheck=expInventoryCheckList.get(i);
            expInventoryCheck.updateOrgId();

            expInventoryCheck.setStockId(expStock.getId());
            expInventoryCheck.setExpCode(expStock.getExpCode());
            expInventoryCheck.setInfoId(expStock.getInfoId());
            expInventoryCheck.setStorageId(expStock.getStorageId());
            expInventoryCheck.setPackageNum(Integer.valueOf(expStock.getPackageNum()));
            expInventoryCheck.setPackageSpec(expStock.getPackageSpec());
            expInventoryCheck.setPackageUnits(expStock.getPackageUnits());
            expInventoryCheck.setPurchasePrice(expStock.getPurchasePrice());
            expInventoryCheck.setBatchNo(expStock.getBatchNo());
            expInventoryCheck.setExpireDate(expStock.getExpireDate());
            expInventoryCheck.preInsert();
            expInventoryCheck.setCreateDate(inventoryDate);
            expInventoryCheck.setUpdateDate(inventoryDate);
            num=num*dao.insert(expInventoryCheck);
        }

        if(expStockInList.size()>0){
            //组成盘点入库数据
            ExpImportMaster expImportMaster=new ExpImportMaster();
            expImportMaster.setStorageId(UserUtils.getUser().getOrgDept().getId());// 入库库房
            expImportMaster.setImportDate(new Date());		                    // 入库日期
            expImportMaster.setSupplier(UserUtils.getUser().getOrgDept().getId());	// 供货方
            expImportMaster.setImportClass("盘点");		// 入库类别
            expImportMaster.setAccountReceivable(0.0);
            expImportMaster.updateOrgId();
            expImportMaster.setDocumentNo(expImportMasterService.createDocumentNo(expImportMaster));
            expImportMaster.preInsert();
            num=num*expImportMasterDao.insert(expImportMaster);
            for(int j=0;j<expStockInList.size();j++){
                //组装expImportDetail数据并保存
                ExpImportDetail expImportDetail=new ExpImportDetail();
                expImportDetail=expImportDetailService.createData(expStockInList.get(j),j,expImportMaster.getId());
                expImportDetail.setQuantity(String.valueOf(expStockInList.get(j).getQuantity()));
                num=num*expImportDetailDao.insert(expImportDetail);
            }
        }

        if(expStockOutList.size()>0){
            //组成盘点出库数据
            ExpExportMaster expExportMaster=new ExpExportMaster();
            expExportMaster.setStorageId(UserUtils.getUser().getOrgDept().getId());// 入库库房
            expExportMaster.setExportDate(new Date());		                    // 入库日期
            expExportMaster.setReceiver(UserUtils.getUser().getOrgDept().getId());	// 供货方
            expExportMaster.setExportClass("盘点");		// 入库类别
            expExportMaster.setImportDate(new Date());
            expExportMaster.setAccountReceivable(0.0);
            expExportMaster.updateOrgId();
            expExportMaster.setDocumentNo(expExportService.createDocumentNo(expExportMaster));
            expExportMaster.preInsert();
            num=num*expExportMasterDao.insert(expExportMaster);
            for(int j=0;j<expStockOutList.size();j++){
                //组装expImportDetail数据并保存
                ExpExportDetail expExportDetail=new ExpExportDetail();
                expExportDetail=expExportService.createData(expStockOutList.get(j),j,expExportMaster.getId());
                expExportDetail.setInventory(Double.valueOf(expStockOutList.get(j).getQuantity()/Double.valueOf(expStockOutList.get(j).getPackageNum())));
                expExportDetail.setQuantity(expStockOutList.get(j).getQuantity());
                num=num*expExportDetailDao.insert(expExportDetail);
            }
        }
        return num;
    }

}